﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.ApplicationBlocks.Data;
using System.Data;

namespace YouHP.SqlServerDAL
{
    public class DatabaseDAL : IDAL.IDatabase
    {

        private void ExecuteSQLScripts(string script,string scriptname)
        {
            
            string[] spliter = new string[] { "\r\nGO", "\r\nGo", "\r\ngo;", "\r\ngO" };
            string[] cmds = script.Split(spliter, StringSplitOptions.RemoveEmptyEntries);
            foreach (string cmd in cmds)
            {
                try
                {
                    SqlHelper.ExecuteNonQuery(Common.ConnStr, CommandType.Text, cmd);
                }
                catch (Exception e)
                {
                    string msg = "\r\n\r\n 执行失败 \r\n \r\n";
                    msg += scriptname + "\r\n\r\n";
                    msg += cmd;
                    msg += "\r\n\r\n";
                    msg += e.ToString();
                    throw new Exception(msg);
                }
            }
        }

        public bool InstallTable()
        {
            return true;
        }

        public bool InstallDatabase()
        {
            this.ExecuteSQLScripts(Properties.Resources.DropAll,"DropAll");
            this.ExecuteSQLScripts(Properties.Resources.CreateTables, "CreateTables");
            this.ExecuteSQLScripts(Properties.Resources.UserSP,"UserSP");
            this.ExecuteSQLScripts(Properties.Resources.DistrictSP,"DistrictSP");
            this.ExecuteSQLScripts(Properties.Resources.MessageSP,"MessageSP");
            this.ExecuteSQLScripts(Properties.Resources.DiseaseSP,"DiseaseSP");
            this.ExecuteSQLScripts(Properties.Resources.SymptomSP,"SymptomSP");
            this.ExecuteSQLScripts(Properties.Resources.TreatmentSP,"TreatmentSP");
            this.ExecuteSQLScripts(Properties.Resources.LabcheckSP, "LabcheckSP");

            this.ExecuteSQLScripts(Properties.Resources.PatientSP,"PatientSP");
            this.ExecuteSQLScripts(Properties.Resources.PatientIndicatorSP, "PatientIndicator");
            this.ExecuteSQLScripts(Properties.Resources.PatientSurvey, "PatientSurvey");
            this.ExecuteSQLScripts(Properties.Resources.PatientSymptomSP, "PatientSymptom");
            this.ExecuteSQLScripts(Properties.Resources.PatientTreatmentSP, "PatientTreatment");
            this.ExecuteSQLScripts(Properties.Resources.PatientWeight, "PatientWeight");

            this.ExecuteSQLScripts(Properties.Resources.GroupSP, "GroupSP");
            this.ExecuteSQLScripts(Properties.Resources.GroupTypeSP, "GroupTypeSP");

            this.ExecuteSQLScripts(Properties.Resources.DistrictData, "DistrictData");

            this.ExecuteSQLScripts(Properties.Resources.ActionSP, "Action");
            this.ExecuteSQLScripts(Properties.Resources.ActivityNews, "ActivityNews");
            this.ExecuteSQLScripts(Properties.Resources.FrientSP, "FrientSP");
            this.ExecuteSQLScripts(Properties.Resources.LogReplySP, "LogReplySP");
            this.ExecuteSQLScripts(Properties.Resources.LogTopicSP, "LogTopicSP");
            this.ExecuteSQLScripts(Properties.Resources.ReplySP, "ReplySP");
            this.ExecuteSQLScripts(Properties.Resources.TopicSP, "TopicSP");
            this.ExecuteSQLScripts(Properties.Resources.Trigger, "TriggerSP");
            this.ExecuteSQLScripts(Properties.Resources.UserActivity, "UserActivity");

            this.ExecuteSQLScripts(Properties.Resources.Function, "Function");
            this.ExecuteSQLScripts(Properties.Resources.CommentSP, "CommentSP");

            this.ExecuteSQLScripts(Properties.Resources.InitData,"InitData");
            return true;
        }

        public bool InstallSP()
        {
            this.ExecuteSQLScripts(Properties.Resources.UserSP, "UserSP");
            this.ExecuteSQLScripts(Properties.Resources.DistrictSP, "DistrictSP");
            this.ExecuteSQLScripts(Properties.Resources.MessageSP, "MessageSP");
            this.ExecuteSQLScripts(Properties.Resources.DiseaseSP, "DiseaseSP");
            this.ExecuteSQLScripts(Properties.Resources.SymptomSP, "SymptomSP");
            this.ExecuteSQLScripts(Properties.Resources.TreatmentSP, "TreatmentSP");
            this.ExecuteSQLScripts(Properties.Resources.LabcheckSP, "LabcheckSP");

            this.ExecuteSQLScripts(Properties.Resources.PatientSP, "PatientSP");
            this.ExecuteSQLScripts(Properties.Resources.PatientIndicatorSP, "PatientIndicator");
            this.ExecuteSQLScripts(Properties.Resources.PatientSurvey, "PatientSurvey");
            this.ExecuteSQLScripts(Properties.Resources.PatientSymptomSP, "PatientSymptom");
            this.ExecuteSQLScripts(Properties.Resources.PatientTreatmentSP, "PatientTreatment");
            this.ExecuteSQLScripts(Properties.Resources.PatientWeight, "PatientWeight");

            this.ExecuteSQLScripts(Properties.Resources.GroupSP, "GroupSP");
            this.ExecuteSQLScripts(Properties.Resources.GroupTypeSP, "GroupTypeSP");

            this.ExecuteSQLScripts(Properties.Resources.DistrictData, "DistrictData");

            this.ExecuteSQLScripts(Properties.Resources.ActionSP, "Action");
            this.ExecuteSQLScripts(Properties.Resources.ActivityNews, "ActivityNews");
            this.ExecuteSQLScripts(Properties.Resources.FrientSP, "FrientSP");
            this.ExecuteSQLScripts(Properties.Resources.LogReplySP, "LogReplySP");
            this.ExecuteSQLScripts(Properties.Resources.LogTopicSP, "LogTopicSP");
            this.ExecuteSQLScripts(Properties.Resources.ReplySP, "ReplySP");
            this.ExecuteSQLScripts(Properties.Resources.TopicSP, "TopicSP");
            this.ExecuteSQLScripts(Properties.Resources.Trigger, "TriggerSP");
            this.ExecuteSQLScripts(Properties.Resources.UserActivity, "UserActivity");
            this.ExecuteSQLScripts(Properties.Resources.CommentSP, "CommentSP");

            this.ExecuteSQLScripts(Properties.Resources.Function, "Function");

            return true;
        }

    }
}
